package com.commpany.servie.basic.order.service.read;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.ArrayList;
import com.commpany.servie.basic.order.api.read.UserOrderReadService;
import com.commpany.jdq.core.common.PageResult;
import com.commpany.jdq.core.orm.Pagination;
import com.commpany.servie.basic.order.business.read.userOrder.UserOrderReadManage;
import com.commpany.servie.basic.order.model.dto.userorder.UserOrderDTO;
import com.commpany.servie.basic.order.converter.UserOrderConverter;
import com.commpany.servie.basic.order.po.UserOrderPO;
import com.commpany.servie.basic.order.model.dto.PageResultDTO;
@Service("userOrderReadService")
public class UserOrderReadServiceImpl  implements UserOrderReadService {
	@Autowired
	private UserOrderReadManage userOrderReadManage;
	
	public UserOrderDTO getUserOrderService(UserOrderDTO inputDTO){
	
	   return UserOrderConverter.toDTO(userOrderReadManage.getUserOrderService(UserOrderConverter.toPO(inputDTO)));
	}
   @Override
	public PageResultDTO<UserOrderDTO> selectUserOrderPageService(UserOrderDTO inputDTO){

		UserOrderPO po = UserOrderConverter.toPO(inputDTO);
		Pagination page = new Pagination();
	    page.setLimitStart(inputDTO.getLimitStart());
		page.setPageNo(inputDTO.getPageNo());
		page.setPageSize(inputDTO.getPageSize());
		Optional.ofNullable(inputDTO.getOrderBy()).ifPresent(page::setOrderBy);
		PageResult<UserOrderPO> poPage = userOrderReadManage.selectUserOrderPageService( po,page);
		List<UserOrderPO> poList = poPage.getList();
		if(Objects.isNull(poList)) {
	    	 return PageResultDTO.<UserOrderDTO>builder().build();
	     }		
		 List<UserOrderDTO> dtoList = poList.stream().map(src-> UserOrderConverter.toDTO(src)).collect(Collectors.toList());
		return 	PageResultDTO.<UserOrderDTO>builder().pageNo(poPage.getPageNo()).pageSize(poPage.getPageSize()).list(dtoList).build();
	}
   @Override
    public int countUserOrderService( UserOrderDTO inputDTO ){
         UserOrderPO po = UserOrderConverter.toPO(inputDTO);
         return userOrderReadManage.countUserOrderService(po);
    }
     @Override
	public List<UserOrderDTO> queryUserOrderService (UserOrderDTO inputDTO){
	    UserOrderPO po = UserOrderConverter.toPO(inputDTO);
	     List<UserOrderPO> list = userOrderReadManage.queryUserOrderService(po);
		   if(Objects.isNull(list)) {
	    	 return new ArrayList<UserOrderDTO>();
	     }
		return list.stream().map(src-> UserOrderConverter.toDTO(src)).collect(Collectors.toList());
	}
}
	